package view;

import java.awt.BorderLayout;


import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import dao.AdminDao;
import dao.CustomerDao;
import view.*;
import modle.Admin;
import modle.Country;
import modle.Customer;
import util.*;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.Toolkit;

public class Customer_Register extends JFrame {

	private JPanel contentPane;
	private JTextField UserName;
	private JTextField Name;
	private JTextField Address;
	private JTextField Telephone;
	private JLabel pw_Label;
	private JLabel noc_Label;
	private JLabel aoc_Label;
	private JLabel ancLabel;
	private JLabel tn_Label;
	private JLabel un_Label;
	private JPasswordField PassWord;
	private JButton btnOrder;
	private JButton btnAdd;
	private JLabel label;
	util.DbUtil dbUtil = new util.DbUtil();
	public static Admin s_currentUser = null;// 保存登陆用户的参数
	private JComboBox comboBox;
	private List<Country> listCountries;
	private Locale locale;
	private ResourceBundle rd;
	private JLabel backgroundlabel;
	private JButton btnBack;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Customer_Register frame = new Customer_Register();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Customer_Register() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(Customer_Register.class.getResource("/img/iconImage.jpg")));
		setTitle("Welcome to Food Corner");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 650, 629);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		ancLabel = new JLabel("Add New Customer");
		ancLabel.setForeground(new Color(205, 133, 63));
		ancLabel.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 21));
		ancLabel.setBounds(176, 13, 272, 38);
		contentPane.add(ancLabel);
		
		UserName = new JTextField();
		UserName.setBounds(310, 80, 225, 30);
		contentPane.add(UserName);
		UserName.setColumns(10);
		
		un_Label = new JLabel("UserName: ");
		un_Label.setForeground(new Color(70, 130, 180));
		un_Label.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 21));
		un_Label.setBounds(46, 77, 203, 30);
		contentPane.add(un_Label);
		
		Name = new JTextField();
		Name.setColumns(10);
		Name.setBounds(310, 214, 225, 30);
		contentPane.add(Name);
		
		Address = new JTextField();
		Address.setColumns(10);
		Address.setBounds(310, 289, 225, 30);
		contentPane.add(Address);
		
		Telephone = new JTextField();
		Telephone.setColumns(10);
		Telephone.setBounds(310, 365, 225, 30);
		contentPane.add(Telephone);
		
		pw_Label = new JLabel("PassWord: ");
		pw_Label.setForeground(new Color(70, 130, 180));
		pw_Label.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 21));
		pw_Label.setBounds(46, 139, 203, 30);
		contentPane.add(pw_Label);
		
		noc_Label = new JLabel("Name of Customer: ");
		noc_Label.setForeground(new Color(70, 130, 180));
		noc_Label.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 21));
		noc_Label.setBounds(46, 211, 239, 30);
		contentPane.add(noc_Label);
		
		aoc_Label = new JLabel("Address of Customer:");
		aoc_Label.setForeground(new Color(70, 130, 180));
		aoc_Label.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 21));
		aoc_Label.setBounds(46, 286, 246, 30);
		contentPane.add(aoc_Label);
		
		tn_Label = new JLabel("Telephone Number:");
		tn_Label.setForeground(new Color(70, 130, 180));
		tn_Label.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 21));
		tn_Label.setBounds(46, 365, 239, 30);
		contentPane.add(tn_Label);
		
		PassWord = new JPasswordField();
		PassWord.setBounds(310, 142, 225, 30);
		contentPane.add(PassWord);
		
		btnAdd = new JButton("Add");
		btnAdd.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 23));
		btnAdd.setBounds(82, 454, 160, 38);
		contentPane.add(btnAdd);
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println("正在注册......");
				registerActionListener(e);
			}
		});
		
		btnOrder = new JButton("Order");
		btnOrder.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//登录过程-----------------------------
				orderActionListener(e);
			}
		});
		btnOrder.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 23));
		btnOrder.setBounds(362, 454, 160, 38);
		contentPane.add(btnOrder);
		
		label = new JLabel("");
		label.setIcon(new ImageIcon(Customer_Register.class.getResource("/img/admin2.png")));
		label.setBounds(554, 110, 64, 103);
		contentPane.add(label);
		
		comboBox = new JComboBox<>();						//组合按钮或可编辑字段和下拉列表的组件
		comboBox.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 17));
		comboBox.addItemListener(new ItemListener() {		//用于接收项目事件的侦听器接口。
			public void itemStateChanged(ItemEvent e) {
				int index = comboBox.getSelectedIndex();	//返回列表中与给定项目匹配的第一个项目。 
					String arr[] = listCountries.get(index).getId().split("-");					//  ???（应该是遇见 - 就分割）
					locale = new Locale(arr[0], arr[1]);//	???   Locale对象代表具体的地理，政治或文化地区。（第一个是语言，第二个是国家）
					rd = ResourceBundle.getBundle("resources.content", locale);		//使用指定的基本名称，区域设置和类加载器获取资源包（这里好像没用类加载器）。 
					
					//rd到底有什么用
					
					try {
						changeLanguage();
					} catch (Exception e1) {
						e1.printStackTrace();
					}
			}
		});
		comboBox.setBounds(520, 17, 98, 32);
		contentPane.add(comboBox);
		
		btnBack = new JButton("Back");
		btnBack.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 19));
		btnBack.setBounds(14, 532, 85, 38);
		contentPane.add(btnBack);
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new Customer_Login().setVisible(true);
				dispose();
			}
		});
		
		fillCountries();
	}
	
	private ImageIcon ResizeImage(String imgpath, JLabel label) {
		// TODO Auto-generated method stub
		ImageIcon img1 = new ImageIcon(imgpath);
		Image img2 = img1.getImage();
		Image img3 = img2.getScaledInstance(label.getWidth(), label.getHeight(), Image.SCALE_SMOOTH);
		ImageIcon img4 = new ImageIcon(img3);
		return img4;
	}

	protected void orderActionListener(ActionEvent e) {
		// TODO Auto-generated method stub
		String username = UserName.getText();
		String password = new String(PassWord.getPassword());
		if (StringUtil.isEmpty(username)) {
			JOptionPane.showMessageDialog(null, "用户名不能为空");
			return;
		}
		if (StringUtil.isEmpty(password)) {
			JOptionPane.showMessageDialog(null, "密码不能为空");
			return;
		}
		
		System.out.println("用户名为：" + username + "  密码为：" + password);
		
		Customer user = new Customer(username, password);
		
		Connection con = null;
		try {
			con = DbUtil.getCon();
			CustomerDao.isUserExist(con, user);
			Customer currentcustomer = CustomerDao.login(con, user);
			if (currentcustomer != null) {
				System.out.println("YES");
				new Customer_Menu(currentcustomer.getId()).setVisible(true);
				this.dispose();
			} else {
				// 弹出提示信息
				JOptionPane.showMessageDialog(null, "用户名或密码错误");
			}
		}catch(Exception e1) {
			e1.printStackTrace();
			JOptionPane.showMessageDialog(null, "出现异常登录失败");
		}finally {
			try {
				// 关闭数据库连接
				dbUtil.closeCon(con);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		
	}

	protected void registerActionListener(ActionEvent e) {
		// TODO Auto-generated method stub 
		
		String username = UserName.getText();
		String password = new String(PassWord.getPassword());
		String name = Name.getText();
		String address = Address.getText();
		String telephone = Telephone.getText();
		
		if (StringUtil.isEmpty(username)) {
			JOptionPane.showMessageDialog(null, "用户名不能为空");
			return;
		}
		if (StringUtil.isEmpty(password)) {
			JOptionPane.showMessageDialog(null, "密码不能为空");
			return;
		}
		if (StringUtil.isEmpty(name)) {
			JOptionPane.showMessageDialog(null, "顾客真实姓名不能为空！");
			return;
		}
		if (StringUtil.isEmpty(address)) {
			JOptionPane.showMessageDialog(null, "顾客地址不能为空");
			return;
		}
		if (StringUtil.isEmpty(telephone)) {
			JOptionPane.showMessageDialog(null, "顾客的电话号码不能为空");
			return;
		}
		if (!StringUtil.isMobileNO(telephone)) {
			JOptionPane.showMessageDialog(null, "顾客的电话号码格式错误");
			return;
		}
		
		Customer user = new Customer(username, password, name, address, telephone);
		
		try {
			if(CustomerDao.isUserExist(DbUtil.getCon(), user))
			{
				JOptionPane.showMessageDialog(null, "该账户已存在，可以直接输入账号密码点菜了");
				return;
			}
		}catch (Exception e1) {
			//弹出提示信息
			JOptionPane.showMessageDialog(null, "出现异常，注册失败！");
			e1.printStackTrace();
		}
		
		try {
			int num = CustomerDao.Add(DbUtil.getCon(), user);
			if(num>0) {
				//弹出提示信息
				JOptionPane.showMessageDialog(null, "注册成功，可以点菜了");
			}else {
				//弹出提示信息
				JOptionPane.showMessageDialog(null, "注册失败");
			}
		} catch (Exception e1) {
			//弹出提示信息
			JOptionPane.showMessageDialog(null, "出现异常，注册失败！");
			e1.printStackTrace();
		}
	}
	
	private void fillCountries() {
		// TODO Auto-generated method stub
		listCountries = new ArrayList<Country>();
		listCountries.add(new Country("en-US", "English"));
		listCountries.add(new Country("zh-CN", "中文"));
		
		for(Country c : listCountries)
			comboBox.addItem(c.getName());
	}

	protected void changeLanguage() {
		// TODO Auto-generated method stub
		
		btnAdd.setText(rd.getString("btnAdd"));
		btnOrder.setText(rd.getString("btnOrder"));
		ancLabel.setText(rd.getString("ancLabel"));
		tn_Label.setText(rd.getString("tn_Label"));
		aoc_Label.setText(rd.getString("aoc_Label"));
		noc_Label.setText(rd.getString("noc_Label"));
		pw_Label.setText(rd.getString("pw_Label"));
		un_Label.setText(rd.getString("un_Label"));
		btnBack.setText(rd.getString("btnBack"));
	}
}
